package com.tianxin.workbao.workBao.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.CoalAdapter;
import com.tianxin.workbao.workBao.bean.Coal;
import com.tianxin.workbao.workBao.dataProvide.CoalProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/14.
 */

public class CoalFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UpdateListViewListener {

    private Context mContent;
    private int page = 1;
    private boolean isEnd = false;//是否到最后的数据
    private CoalAdapter mCoalAdapter;
    private CoalProvide mCoalProvide;
    private List<Coal> mCoals;

    public static CoalFragment newInstance() {

        Bundle args = new Bundle();

        CoalFragment fragment = new CoalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContent = getContext();
        mCoals = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initRootView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.recircle_view_content, container, false);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(
                R.color.recycler_color1,
                R.color.recycler_color2,
                R.color.recycler_color3,
                R.color.recycler_color4);

        mCoalAdapter = new CoalAdapter(mCoals, getContext());
        mRecyclerView.setAdapter(mCoalAdapter);
        //设置布局管理器
        mLayoutManager = new GridLayoutManager(getContext(), spanCount);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mCoalAdapter.getItemColumnSpan(position);
            }
        });
        mRecyclerView.setLayoutManager(mLayoutManager);

        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initData(boolean isSavedNull) {
        onRefresh();
    }

    @Override
    public void initEvents() {
        //设置监听RecyclerView 的滚动监听事件
        mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener());
    }

    @Override
    public void search(String query) {
       getCoalProvide().search(query);
    }

    //SwipeRefreshLayout.OnRefreshListener
    @Override
    public void onRefresh() {
        if (mSwipeRefreshLayout.isRefreshing() == false) {
            mSwipeRefreshLayout.setRefreshing(true);
            getCoalProvide().getCoals(page =1);
        }
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefreshData(Object result) {
        mCoals.clear();
        if (result instanceof List && result != null) {
            mCoals.addAll((List<Coal>) result);
        }
        mCoalAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMoreData(Object result) {
        if (result instanceof List && result != null) {
            mCoals.addAll((List<Coal>) result);
            mCoalAdapter.notifyDataSetChanged();
        }
    }


    class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {

        private int lastVisibleItem;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mCoalAdapter.getItemCount()) {
                onLoadMore();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
        }
    }

    //加载更多
    private void onLoadMore() {
        if (isEnd) {
            mSwipeRefreshLayout.setRefreshing(false);
            showMessage(getResources().getString(R.string.no_data));
        } else {
            if (!mSwipeRefreshLayout.isRefreshing()) {
                page++;
                getCoalProvide().getCoals(page);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }

    public CoalProvide getCoalProvide() {
        if (mCoalProvide == null) {
            mCoalProvide = new CoalProvide(getContext(),this);
        }
        return mCoalProvide;
    }
}
